(function($){
    $.extend({
        loadPage: function($url){
            $.addJsSource("html/js/common/boxy.js");
            Boxy.load($url, {
                'modal': true,
                'afterShow': function(){
                    $("#seccodeDiv").removeClass("hidden");
                }
            });
        },
        loadHtml: function($url, container_id){
            $content = $("#" + container_id);
            
            if (!$url) {
                return false;
            }
            $.showPageInfo("#page_info", "loading......", 100, false);
            $.ajax({
                url: $url,
                type: 'GET',
                cache: false,
                dataType: 'html',
                success: function(data){
                    $content.css({
                        'display': 'none',
                        'float': 'right'
                    }).empty();
                    $content.html(data).show('fast');
                    $("#page_info").fadeOut(1000);
                    $("#" + container_id + " a").not('.action_edit_fast a,.delete_btn a,.action_edit a,.action_delete a,.action_preview a').click(function(e){
                        e.preventDefault();
                        $hr = $(this).attr("href");
                        $.loadHtml($hr, container_id);
                        return false;
                    });
                }
            });
            
            return false;
        },
        ajaxSubmit: function($form, func){
            $data = $form.serializeArray();
            $url = $form.attr("action");
            $.ajax({
                url: $url,
                data: $data,
                type: 'post',
                async: false,
                cache: true,
                dataType: 'json',
                success: function(data){
                    func(data);
                }
            });
        },
        showPageInfo: function(info_area, data, time, is_along){
            $(info_area).html("<span>" + data + "</span>").fadeIn(time, function(){
                setTimeout(function(){
                    if (is_along) 
                        $(info_area).fadeOut(1000);
                }, 10000);
                
            });
        },
        getCitysByPid: function($pid){
            $result = true;
            $url = "/common/getCitysByPcode";
            $.ajax({
                url: $url,
                data: {
                    'pcode': $pid
                },
                type: 'POST',
                async: false,
                dataType: 'json',
                success: function(data){
                    $result = data;
                },
                error: function(data){
                    $result = false;
                }
            });
            return $result;
        },
        formReset: function(form){
            $(form + " :input:not([type='button']):not([type='submit'])").each(function(){
                $(this).val("");
            });
        },
        checkboxEvent: function(){
        
            $(":checkbox[name='check_all']").each(function(){
                $(this).click(function(){
                
                    if ($(this).attr("checked")) {
                        $(this).parents("table").find(":checkbox").add(":checkbox[name='check_all']").checkAll()
                    }
                    else {
                        $(this).parents("table").find(":checkbox").unCheck();
                    }
                });
            });
            
        },
        doBatchAction: function(func){
            $url = $("[name=action]").val();
            var id = "";
            if ($url == "0") {
                $.showPageInfo("#page_info", '请选择动作', 500, true);
                return false;
            }
            else {
                var checkboxs = $(":checkbox[name='check_all']").parents("table").find("tbody :checkbox:checked");
                if (checkboxs.size() == 0) {
                    $.showPageInfo("#page_info", '请选择要执行的项', 500, true);
                    return false;
                }
                $.each(checkboxs, function(){
                    id += $(this).val() + ',';
                })
                
                id = id.substring(0, id.length - 1);
                
                $.ajax({
                    url: $url,
                    data: {
                        'item': id
                    },
                    type: 'post',
                    dataType: 'json',
                    success: function(data){
                        if (func) {
                            func(data);
                        }
                        else {
                            $.showPageInfo("#page_info", data.info, 500, true);
                            checkboxs.each(function(){
                                $(this).parents('tr').fadeOut(500, function(){
                                    $(this).empty();
                                });
                            });
                        }
                    }
                    
                });
            }
        },
        baseAction: function($url, param, func){
            $.ajax({
                url: $url,
                data: param,
                type: 'post',
                dataType: 'json',
                success: function(data){
                    if (func) {
                        func(data);
                    }
                    else {
                        return false;
                    }
                }
            });
        },
        getFilter: function($url, func){
            if ($url) {
                if (func) {
                    this.doAction($url, func);
                }
                else {
                    $.loadHtml($url, 'content_page');
                    return false;
                }
            }
            else {
                return false;
            }
        },
        fastEdit: function(func){
            var $edit = $(".action_edit_fast > a");
            $edit.unbind('click').bind('click', function(e){
                $(this).parents('tr').siblings('tr').andSelf().css('display', 'none');
                $(this).parents('tr').next().css('display', '');
                if (func) 
                    func($(this));
                return false;
            });
            $("[name=fast_cancel]").unbind('click').click(function(e){
                $(this).parents('tr').css('display', 'none');
                $(this).parents('tr').siblings('.table_list').css('display', '');
                return false;
            });
            $("[name=fast_save]").unbind('click').click(function(e){
                $(this).before("<div class='left busying'></div>");
                $this = $(this);
                $.ajaxSubmit($(this).parents('form'), function(data){
                    $this.prev('div.busying').fadeOut(800, function(){
                        $(this).remove();
                    });
                    $.showPageInfo("#page_info", data.info, 500, true);
                });
                return false;
            });
        },
        edit: function(){
            $(".action_edit>a").unbind('click').bind('click', function(){
                var $url = $(this).attr('href');
                var $item = $(this).parents('.row_action').attr('num');
                $.loadHtml($url + "?item=" + $item, 'content_page');
                return false;
            });
        },
        delObject: function($action){
            $(".action_delete > a").click(function(e){
                if (!confirm('是否要删除这一项')) {
                    return false;
                }
                var $id = $(this).parents(".row_action").attr('num');
                $param = {
                    'item': $id
                };
                $this = $(this);
                $.baseAction($action, $param, function(data){
                    $.showPageInfo("#page_info", data.info, 500, true);
                    $this.parents('tr').fadeOut(500, function(){
                        $(this).empty();
                    });
                });
                return false;
            });
        },
        search: function($url){
            $.loadHtml($url, 'content_page');
        }
    });
    
})(jQuery)
